<?php 
if (isset($_GET["view"])) {
	require_once("applicants_views.inc");
	$view = $applicants_views[$_GET["view"]];
	if (isset($view[0]) && is_array($view[0])) {
		$fields = $view[0];
		$filters = $view[1];
	} else {
		$fields = $view;
		$filters = array();
	}
	foreach ($fields as $field=>$editable) {
		$field_name = "field__".str_replace(".","$",str_replace("#","$",$field));
		$_POST[$field_name] = "1";
		if ($editable)
			$_POST[$field_name."__edit"] = "1";
	}
	$filter_counter = 0;
	foreach ($filters as $field=>$condition) {
		$id = $filter_counter++;
		$_POST["filter_".$id] = "field__".str_replace(".","$", str_replace("#", "$", str_replace("/", "$", $field))).$condition;
	}
}


function has_filter($f) {
	global $filters;
	foreach ($filters as $filter) {
		switch ($filter["type"]) {
			case "column": if ($filter["field"]==substr($f->get_show_checkbox_name(),7)) return true; break;
		}
	}
	return false;
}
function is_shown($f) {
	$name = $f->get_show_checkbox_name();
	return isset($_POST[$name]) && $_POST[$name]=="1";
}
function has_edit($f) {
	$name = $f->get_edit_checkbox_name();
	return isset($_POST[$name]) && $_POST[$name]=="1";
}

function first_pos_of($str,$chars) {
	$min = false;
	for ($i = 0; $i < strlen($chars); ++$i) {
		$pos = strpos($str,$chars[$i]);
		if ($pos !== false) {
			if ($min === false || $pos < $min)
				$min = $pos;
		}
	}
	return $min;
}

$filters = array();
$or_filters = array();
foreach ($_POST as $pname=>$filter) {
	if (substr($pname,0,10)=="or_filter_") {
		$or_filters[$filter] = substr($pname,10);
		continue;
	}
	if (substr($pname,0,7)<>"filter_") continue;
	if ($filter=="") continue;
	$filter_id = substr($pname,7);
	//echo "Filter ".$filter_id.": ".$filter."<br/>";

	if (substr($filter,0,7)=="field__") {
		$filter = substr($filter,7);
		//echo "Filter '".$filter."': ";
		$i = first_pos_of($filter, " =<>!");
		if ($i === false) { echo "Invalid filter (no condition): ".text2html($filter)."<br/>"; continue; }
		$field = substr($filter,0,$i);
		$condition = substr($filter,$i);
		//echo " field='".$field."' condition='".$condition."'<br/>";
		array_push($filters, array("id"=>$filter_id,"type"=>"column","field"=>$field,"condition"=>$condition,"or"=>null));
	} else {
		echo "Invalid filter (unknown type): ".text2html($filter)."<br/>"; continue;
	}
}
// put the or in the linked filter
for ($i = 0; $i < count($filters); $i++) {
	$filter_id = $filters[$i]["id"];
	if (isset($or_filters[$filter_id])) {
		$or_id = $or_filters[$filter_id];
		for ($j = 0; $j < count($filters); $j++) {
			if ($j == $i) continue;
			if ($filters[$j]["id"] == $or_id) {
				$filters[$i]["or"] = &$filters[$j];
				break;
			}
		} 
	}
}
// remove the or from the root
foreach ($or_filters as $container_id=>$or_id) {
	for ($i = 0; $i < count($filters); $i++)
		if ($filters[$i]["id"]==$or_id) {
			array_splice($filters, $i, 1);
			$i--;
			break;
		}
}

$sort = null;
if (isset($_POST["sort"])) {
	$sort = $_POST["sort"];
	if ($sort == "") $sort = null;
}
?>